package com.zml.controller;

import com.zml.entity.Blog;
import com.zml.entity.sync.SysConfig;
import com.zml.service.BizDataService;
import com.zml.service.BlogService;
import com.zml.utils.DateUtil;
import com.zml.utils.JsonResp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author <a href="mailto:zhaominglin@wxchina.com">minglin.Zhao</a>
 * @Date 2017/10/11
 * @Version 1.0.0
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private SysConfig sysConfig;

    @Autowired
    private BizDataService bizDataService;

    private static final Logger logger = LogManager.getLogger(BlogController.class);

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    @ResponseBody
    public JsonResp blogTest(@RequestBody Blog blog){
        try {
            blogService.addBlog(blog);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("error!!!!!");
        }
        return JsonResp.success();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResp getBlog(@PathVariable("id") int id){
        Blog blog = blogService.findById(id);
        return JsonResp.success(blog);
    }

    /**
     *  上传demo
     *  success : 0 | 1,           // 0 表示上传失败，1 表示上传成功
     *  message : "提示的信息，上传成功或上传失败及错误信息等。",
     *  url     : "图片地址"        // 上传成功时才返回
     * */
    @RequestMapping(value = "/uploadfile",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> demo(@RequestParam(value = "editormd-image-file", required = false) MultipartFile file, HttpServletRequest request) throws Exception{
        Map<String,Object> resultMap = new HashMap<String,Object>();
        String uploadPath = sysConfig.getImgUploadPath();
        String fileName = DateUtil.getCurrentDateStr() + "." + file.getOriginalFilename().split("\\.")[1];
        File targetFile = new File(uploadPath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        //保存
        try {
            file.transferTo(new File(uploadPath + fileName));
            resultMap.put("success", 1);
            resultMap.put("message", "上传成功！");
            resultMap.put("url","/static/upload/" + fileName);
        } catch (Exception e) {
            resultMap.put("success", 0);
            resultMap.put("message", "上传失败！");
            e.printStackTrace();
        }
        return resultMap;
    }
}
